package com.keepers.repair.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keepers.repair.R;
import com.keepers.repair.adapter.LineAndPointAdapter;
import com.keepers.repair.helper.ConsValue;
import com.keepers.repair.model.GetTaskListModel;
import com.keepers.repair.net.DataLoader;
import com.keepers.repair.net.LogUtil;
import com.keepers.repair.utils.AlertToast;
import com.keepers.repair.view.TitleBar;

/**
 * auth aboom
 * date 2018/11/17
 */
public class TaskListDetailActivity extends BaseActivity {

    private GetTaskListModel    getTaskListModel;
    private LineAndPointAdapter adapter;
    private TextView            emptyMessage;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tasktodaydetail_layout);
        getTaskListModel = (GetTaskListModel) getIntent().getSerializableExtra(ConsValue.INTENT_KEY);
        LogUtil.e(JSONObject.toJSONString(getTaskListModel));
        TitleBar titleBar = f(R.id.title_bar);
        titleBar.initializeHeader(getTitleStr());
        TextView taskName = f(R.id.task_name);
        taskName.setText(getTaskListModel.TaskName);
        TextView taskType = f(R.id.task_type);
        taskType.setText(getTaskListModel.WordName);
        TextView lineName = f(R.id.line_name);
//        lineName.setText();
        TextView startData = f(R.id.start_data);
        startData.setText(getTaskListModel.StartData);
        TextView endData = f(R.id.end_data);
        endData.setText(getTaskListModel.EndData);
        TextView automatedTv = f(R.id.automated);
        if ("shift".equals(getTaskListModel.Automated)) {
            automatedTv.setText("轮班");
        } else if ("week".equals(getTaskListModel.Automated)) {
            automatedTv.setText("周");
        } else {
            automatedTv.setText("天");
        }
        TextView sayTime = f(R.id.say_time);
        if (!TextUtils.isEmpty(getTaskListModel.SayTime) && !"0".equals(getTaskListModel.SayTime)) {
            sayTime.setText(getTaskListModel.SayTime + "分钟");
        }
        emptyMessage = f(R.id.empty_message);
        LinearLayout lyPatrolTimes = f(R.id.ly_patrolTimes);
        LinearLayout lyPatrolSpace = f(R.id.ly_patrolSpace);
        LinearLayout lySaySpace = f(R.id.ly_saySpace);
        TextView tvPatrolTimes = f(R.id.patrolTimes);
        TextView tvPatrolSpace = f(R.id.patrolSpace);
        RecyclerView recyclerView = f(R.id.list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new LineAndPointAdapter(R.layout.item_lineplace,getTaskListModel.WordName);
        recyclerView.setAdapter(adapter);
        if ("无目的巡逻".equals(getTaskListModel.WordName)) {
            emptyMessage.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            if ("乱序巡逻".equals(getTaskListModel.WordName)) {
                lySaySpace.setVisibility(View.GONE);
                lyPatrolTimes.setVisibility(View.VISIBLE);
                lyPatrolSpace.setVisibility(View.VISIBLE);
                tvPatrolTimes.setText("-");
                tvPatrolSpace.setText("-");
                if (!TextUtils.isEmpty(getTaskListModel.PatrolTimes) && !"0".equals(getTaskListModel.PatrolTimes)) {
                    tvPatrolTimes.setText(getTaskListModel.PatrolTimes);
                }
                if (!TextUtils.isEmpty(getTaskListModel.IntervalTime) && !"0".equals(getTaskListModel.IntervalTime)) {
                    tvPatrolSpace.setText(getTaskListModel.IntervalTime);
                }
            }
        }

        loadData();
    }

    public void loadData() {
        rxHelper.pend(DataLoader.getLineAngdPlaceInforByID(getTaskListModel.Gid, lineAndPlaceListModel -> {
            if (lineAndPlaceListModel == null || lineAndPlaceListModel.LineAndPlace == null) {
                AlertToast.show("获取路线数据发生异常...");
                return;
            }
            adapter.setNewData(lineAndPlaceListModel.LineAndPlace);
            emptyMessage.setVisibility(lineAndPlaceListModel.LineAndPlace.size() == 0 ? View.VISIBLE : View.GONE);
        }));
    }


    private String getTitleStr() {
        if (getTaskListModel.type == 0) {
            return "今日任务详情";
        }
        if (getTaskListModel.type == 1) {
            return "明日任务详情";
        }
        return "";
    }
}
